<?php namespace Common\Model;
use Hdphp\Model\Model;
class Brand extends Model{
	protected $table = 'brand';
	protected $validate = array(
		array('bname','required','商品名称不能为空',3,3)
	);
	protected $auto=array(
		array('blogo','getlogo','method',3,1)
	);
	
	//品牌添加
	public function store(){
		if($this->create()){
			$this->add();
			return true;
		}
		return false;
	}
	
	//修改
	public function edit(){
		if(!$this->create()) return false;
		$this->save();
		return true;
	}
	
	//上传logo
	public function getlogo(){
		$oldLogo = Q('post.blogo');
//		P($_FILES);
		if($_FILES['blogo']['error'] == 4){
			if($oldLogo){
				return $oldLogo;
			}
			return '';
		}
		$files = Upload::type('jpg,jpeg,png,gif')->make();
		if($files){
			if($oldLogo){
				unlink($oldLogo);
				$path = str_replace('_thumb','',$oldLogo);
				unlink($path);
			}
			//缩略
			$thumbImg = str_replace(".{$files[0]['ext']}","_thumb.{$files[0]['ext']}",$files[0]['path']);
			$img = Image::thumb($files[0]['path'],$thumbImg,200,50,1);
			return $img;
		}
		$this->error = Upload::getError();
	}
}
